﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using ResourceClasses;

namespace GeneralStorageClasses
{
	/// <summary>
	/// Story manager. Manages the stories.
	/// </summary>
	public class StoryManager
	{
		private Dictionary<int,Story> storylist;
		private int size;
		private static StoryManager m_instance;

		public StoryManager (string path)
		{
			size = 0;
			m_instance = this;
			storylist = new Dictionary<int,Story> ();
			XmlDocument xmlDoc = new XmlDocument ();

			//Good luck, have fun
			try {
				xmlDoc.Load ("Assets/Data/" + path);
			
				XmlNodeList listOfStories = xmlDoc.GetElementsByTagName ("Story");

				foreach (XmlNode storyNode in listOfStories) {
					XmlNodeList listOfStoryAttrib = storyNode.ChildNodes;
					Story loadedStory = new Story ();
					foreach (XmlNode atrribs in listOfStoryAttrib) {
						setStoryObject (ref loadedStory, atrribs);
					}
					storylist.Add (loadedStory.ID, loadedStory);
				}
				for (int i = 0; i < storylist.Count; i++) {
					Debug.Log ("Loaded Story:\n" + storylist [i].ToString ());
				}

				size = storylist.Count;
			} catch (FileNotFoundException) {
				Debug.LogError ("ERROR: Cannot find " + path + "\nIn Assets/Data/" + path);
			}
		}

		/// <summary>
		/// Sets the story object.
		/// </summary>
		/// <param name="tempStory">Temp story.</param>
		/// <param name="atrribs">Atrribs.</param>
		private void setStoryObject (ref Story tempStory, XmlNode atrribs)
		{
			switch (atrribs.Name) {
			case "ID":
				{
					tempStory.ID = System.Convert.ToInt32 (atrribs.InnerText);
					break;
				}
			case "Title":
				{
					tempStory.Title = atrribs.InnerText;
					break;
				}
			case "Information":
				{
					tempStory.Info = atrribs.InnerText;
					break;
				}
			case "Objectives":
				{
					tempStory.Objectives = atrribs.InnerText;
					break;
				}
			case "Rewards":
				{
					List<Resource> resList = new List<Resource> ();
					foreach (XmlNode resources in atrribs) {
						string name = resources ["Type"].InnerText;
						int amount = System.Convert.ToInt32 (resources ["Amount"].InnerText);
						resList.Add (new Resource (name, amount));
					}
					tempStory.rewAcc = new Reward (resList.ToArray ());
					break;
				}
			}
		}
	
		/// <summary>
		/// Gets the <see cref="GeneralStorageClasses.StoryManager"/> with the specified storyID.
		/// </summary>
		/// <param name="storyID">Story I.</param>
		public Story this [int storyID] {
			get {
				try {
					return storylist [storyID];
				} catch (KeyNotFoundException) {
					Debug.LogError ("ERROR: Story ID not found at " + storyID);
					return null;
				}
			}
		}

		/// <summary>
		/// Gets the instance.
		/// </summary>
		/// <value>The instance.</value>
		public static StoryManager Instance {
			get {
				if (m_instance == null) {
					Debug.LogError ("ERROR: No instance of StoryManager found!");
				}
				return m_instance;
			}
		}

		/// <summary>
		/// Gets the size of the array.
		/// </summary>
		/// <value>The size.</value>
		public int Size {
			get {
				return size;
			}
		}
	}
}
